
package network.httpsServer;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;

import network.utils.ProxyConstants;

/**
 * Read from external server and write to browser through tunnel socket..
 * 
 * @author http://arunava.in
 * 
 */
class HttpsServerOutgoingThread extends Thread {
	private Socket incoming;

	private Socket outgoing;


	HttpsServerOutgoingThread(final Socket in, final Socket out) {
		incoming = in;
		outgoing = out;
	}

	public void run() {
		if (ProxyConstants.logLevel >= 1) {
			System.out.println(new Date()
					+ " HttpsServerOutgoingThread Started :: "
					+ currentThread());
		}

		final byte[] buffer = new byte[ProxyConstants.MAX_BUFFER];
		int numberRead = 0;
		OutputStream ToClient;
		InputStream FromClient;

		try {
			ToClient = outgoing.getOutputStream();
			FromClient = incoming.getInputStream();
			while (true) {
				numberRead = FromClient.read(buffer);
				if (numberRead == -1) {
					outgoing.close();
					incoming.close();
					break;
				}
				ToClient.write(buffer, 0, numberRead);
			}

			if (ProxyConstants.logLevel >= 1) {
				System.out.println(new Date()
						+ " HttpsServerOutgoingThread Finish :: "
						+ currentThread());
			}

		} catch (final Exception e) {

		}
	}
}
